//
//  FKMyCollectVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/28.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKMyCollectVC.h"
#import "FKMyCollectCell.h"
#import "FKCommodityDetailVC.h"

@interface FKMyCollectVC ()

@property (nonatomic, strong) NSArray *datas;
@property(nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableDictionary *param;
@property (nonatomic, strong) UIView *emptyView;

@end

@implementation FKMyCollectVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (NSMutableDictionary *)param
{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
        _param[@"pagesize"] = @(10);
    }
    return _param;
}

- (UIView *)emptyView
{
    if (!_emptyView) {
        _emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, 200)];
        
        UIView *container = [[UIView alloc] init];
        [_emptyView addSubview:container];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Emptyorder"]];
        [container addSubview:imageView];
        
        UILabel *label = [UILabel fk_labelWithFont:fk_adjustFont(16) textColor:fkColor999999 textAlignment:NSTextAlignmentCenter text:@"您暂时还没有收藏商品~"];
        [container addSubview:label];
        
        UIButton *bottomBtn = [UIButton fk_btnWithTarget:self action:@selector(bottomBtnClicked) font:fk_adjustFont(15) normalTitle:@"去逛逛" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_main"] selectedBgImg:nil];
        [container addSubview:bottomBtn];
        [bottomBtn fk_viewCornerRadius:fk_adjustW(20) borderWidth:0 borderColor:nil];
        
        CGFloat imageW = imageView.image.size.width;
        CGFloat imageH = imageView.image.size.height;
        CGFloat imageX = (fkScreenW - imageW) * 0.5;
        imageView.frame = CGRectMake(imageX, 0, imageW, imageH);
        CGFloat titleH = [label.text sizeWithAttributes:@{NSFontAttributeName : label.font}].height;
        label.frame = CGRectMake(0, imageView.fk_bottom + 20, fkScreenW, titleH);
        CGFloat bottomW = fk_adjustW(225);
        CGFloat bottomH = fk_adjustW(40);
        CGFloat bottomX = (fkScreenW - bottomW) * 0.5;
        bottomBtn.frame = CGRectMake(bottomX, label.fk_bottom + 50, bottomW, bottomH);
        
        __block CGFloat containerH = bottomBtn.fk_bottom;
        [container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.offset(0);
            make.width.mas_equalTo(fkScreenW);
            make.height.mas_equalTo(containerH);
        }];
    }
    return _emptyView;
}

- (void)bottomBtnClicked
{
    self.tabBarController.selectedIndex = 1;
    [self.navigationController popToRootViewControllerAnimated:false];
}

//- (UIView *)emptyView
//{
//    if (!_emptyView) {
//        _emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, fkScreenH)];
//
//        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Emptyorder"]];
//        [_emptyView addSubview:imageView];
//        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.centerY.offset(0);
//            make.width.mas_equalTo(fk_adjustW(imageView.image.size.width));
//            make.height.mas_equalTo(fk_adjustW(imageView.image.size.height));
//        }];
//
//        UILabel *label = [[UILabel alloc] init];
//        [_emptyView addSubview:label];
//        label.font = fk_adjustFont(16);
//        label.text = @"您暂时还没有收藏商品~";
//        label.textColor = fkColor999999;
//        [label mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(imageView.mas_bottom).offset(20);
//            make.centerX.offset(0);
//        }];
//    }
//    return _emptyView;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的收藏";
    
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKMyCollectCell *cell = [FKMyCollectCell cellWith:tableView];
    cell.data = self.datas[indexPath.row];
    fkWeakSelf(self);
    cell.deleteBlock = ^(NSDictionary * _Nonnull dict) {
        [weakself fk_alertWithTitle:@"确定要取消收藏吗" confirm:^{
            [weakself deleteCollect:dict[@"id"]];
        }];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(114);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKCommodityDetailVC *vc = [[FKCommodityDetailVC alloc] init];
    NSDictionary *data = self.datas[indexPath.row];
    vc.itemId = data[@"item_id"];
//    vc.package = ;
    fkPushNav(vc);
}

#pragma mark - 网络请求
- (void)loadData
{
    self.page = 1;
    self.param[@"page"] = @(self.page);
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=collect_list" param:self.param success:^(FKBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        if (result.isSuccess) {
            self.datas = result.data[@"list"];
            if (self.datas.count > 0) self.page++;
            [self.tableView reloadData];
        }
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    } failure:^(NSError *error) {
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)loadMoreData
{
    self.param[@"page"] = @(self.page);
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=collect_list" param:self.param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            NSArray *newDatas = result.data[@"list"];
            if (newDatas.count > 0) {
                [self.tableView.mj_footer endRefreshing];
                NSMutableArray *array = [NSMutableArray arrayWithArray:self.datas];
                [array addObjectsFromArray:newDatas];
                self.datas = array;
                [self.tableView reloadData];
                self.page++;
            } else {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)deleteCollect:(NSString *)collectId
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"collect_id"] = collectId;
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=del_collect" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showSuccessWithStatus:@"取消收藏成功"];
            [self loadData];
        }
    } failure:nil];
}

@end
